package common.cacher
{
    import com.netease.protobuf.Int64;

    import kernel.core.Core;
    import kernel.events.ServerEvent;

    import proto.client.LongList;
    import proto.client.Player;
    import proto.client.PlayerList;

    public class PlayerCacher extends HttpCacher
    {
        private static var _ins:PlayerCacher;

        public static const MAX:int = 20;
        private var _queueToPut:Array;

        private var _bgeting:Boolean = false;
        private var _queueGeting:Array;

        public function PlayerCacher()
        {
            _queueToPut = [];
            _queueGeting = [];
            super (0x000000 , null , ["id"] , 2 * 60 * 1000);
        }

        public static function get instance():PlayerCacher
        {
            if (!_ins)
            {
                _ins = new PlayerCacher ();
            }
            return _ins;
        }

        public function getPlayerList(idlist:Array , callback:Function):void
        {
            var len:int = idlist.length;
            for (var i:int = 0 ; i < len ; ++i)
            {
                var strid:String = idlist[i].toString ();
                var data:Player = getObj (strid);
                if (data != null)
                {
                    callback.call (null , data);
                }
                else
                {
                    addCallback (strid , callback);
                    if (_queueGeting.indexOf (strid) == -1 && _queueToPut.indexOf (strid) == -1)
                    {
                        _queueToPut.push (strid);
                    }
                }
            }
            sendNextStep ();
        }

        override public function getCache(id:* , callback:Function):void
        {
            getPlayerList ([id] , callback);
        }

        protected function sendNextStep():void
        {
            if (_bgeting)
            {
                return;
            }

            var size:int = Math.min (MAX , _queueToPut.length);
            if (size > 0)
            {
                _queueGeting = _queueToPut.splice (0 , size);
                sendMessage (_queueGeting);
                _bgeting = true;
            }
        }

        override protected function sendMessage(id:*):void
        {
            var getNames:LongList = new LongList ();
            if (id is Array)
            {
                var len:int = (id as Array).length;
                for (var i:int = 0 ; i < len ; ++i)
                {
                    getNames.value.push (Int64.parseInt64 (id[i].toString ()));
                }
            }
            else
            {
                getNames.value.push (Int64.parseInt64 (id.toString ()));
            }
            Core.httpSend (_head , getNames , ongetData , id);
        }

        override protected function ongetData(event:ServerEvent):void
        {
            if (event.berror)
            {
                if (event.ident is Array)
                {
                    var listss:Array = event.ident as Array;
                    var lens:int = listss.length;
                    for (var j:int = 0 ; j < lens ; ++j)
                    {
                        callbackNULL (listss[i].toString ());
                    }
                }
                else
                {
                    callbackNULL (event.ident.toString ());
                }
                return;
            }
            var lis:PlayerList = event.createMessage (PlayerList) as PlayerList;
            var len:int = lis.player.length;
            deleteOlded ();
            for (var i:int = 0 ; i < len ; ++i)
            {
                decodeData (lis.player[i]);
            }
            _bgeting = false;
            sendNextStep ();
        }
    }
}
